package com.ll.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;

import com.ll.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private String app_id = "2016100200645312";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private static String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC/EgCpeBpYoGz9bDz4VVX3YhtbTBGY6qUB3l0ZKSb8V9mnY/gX/kOXpY9p6mn2lngToxNlJ8G9FE/E+zetl4VPjerbX3wDhENOGNON83tahPLSNHIQ4lJUzlIUfK5UOXSFHBC6m4HPWYVs3xU0CjctilRDukGAggCFQw8H22q4skBsvWX3h+BGDjTYM9LATx115QWX7T9HKUwc8W66k2kW5q8/+sLURvxB8zO5sjH7XAN3hDFSkDt00teWNpB5wAG4nJgPCAEkKHA6F+chtma7s93v3FnAKdQMJp69Vgh+h2tckXzuc+6TlohKINg41alUw3T+zMYSqJd6EYh+rVQ5AgMBAAECggEACwI+h4Uj/3c+0FssKCLCkT5V1SzM1sKuPy1Qr+JvHJrzL3HTGY7M1i/z02ZuNIb25CrBM1LXFs3UysSusJHKXIsQvRMDUjCMsDM9/S/KK6z9UJ8080QW+kvo7sVARhoJXKIBVL8g+5Fs3pj738O26ijOMhjJLV5QKV1zwEAkJgAFkxokyIZG6P6DLqhxXs6/Dpf7SnFU22ofFMglynp2A5/JByaaWx2wXpTF0cutLmKmH9xVAw8hrR/oLeBOxEq9MGYdoo/gscIGx7nG8eZgHOPt8KznAHJn1mfm9gJGqSmW/Xgj65fCY4v8ZZwgd+1j2+bl9d9HFVMm1J3hKXHvAQKBgQD2oEtZFnc4t6+9KANliyucNxmeGOpFleIcwoX3uhuAonhm6bdMETX0Z/wNaKrbhSP/BRxTWU3NhAFdNg77IfpOC5S/mN4yhbgP6b2G9u2tWUsUKNagY3mtZYduGyNtsN6lhzmLixMiOQqAkPIW7yI/fbz5pmsJltiwf3zqwBc+pwKBgQDGVSR/rmq6C9T2y5q60VWMRekbZkqdta+xmNhOsC4cpwVKCUWIAFoDw+wA/WbdX3NfZTjkhf7aq0mGdUYvEJ6M9GZaCvEt9zQi6C39v5NvZnlo7EDdVqBnI2Kfa1WK12etfYm/PJ7D/nQraWsWtTxgiDG+0YSnj7p3zt8SZoMSHwKBgQCJ8EeUUVNnPp2lT27u6XL/0XWbjlXhi55G9amWT9o4DScIcGnburiRtWMtYMBHPWVPql0YZRd1/gIBT037uhpUcnfE3cxRN6cmB1bA2PVAgnPSl7DLTjfs+p9PvxAGtukgbcyKtsOB3aN5Gxi8KQ3fswTyINk3KM6TkQqeBcg6ewKBgB2/i5fzK2L4f1r9MxouZ+yAV/SaJTr+k3nJ+TfkSSZXDv4J4foCQ34pFu8XEyTFOmLmpd/xEUbvRjn6wSoyDfYKeoeq+H3yhElwFeXnpE+jSTapqRkJ80lFD3fl+5C1uoFVQSIED7RKKFaSm1Nun73C7+M1PfZ8InXx+5eg9Kv1AoGAFweLAFvwXWdX2n11rFN7vPpPqzjaFxSb8MW3Ajuh0RjM6SHNfAwSZfl9E3lb7RgphT4gcmr9BKv+090CS0zLv8zHaOYZ53AZoJaoHg+kmMO1+bGM4m7N50uZjMILQoI6q4ec4RXpNjO8h4Ario+eIoLvDks1T99NVZa26eJVZMs=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。


    private String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuuEuAGnF8DfR1UmFmP2MOqAoyKqewdIdlkprSio1uYrmD2bxaD02mssEAWkX5r5nyWbmBYzKqFIV5RJnKQ5Q7gEUadke+Cs5hwBVPS3UtkF/rkmArNkNl+atUc/jW0TFo5AZtY+OVlW7M4UI0wIVO4rhtOV0Xsj1i2dRq5ifCzckRccLw7b4T04KLb8PThbhxhiWpOO7qDcbmIyfLL7wvd9VJ0yNOWdoiCs0xJ3rNZmIUU/fgr8IVsIyTUDly0k1BY3BKqVSJ7UzjWGWe9M0jxhr2l7xZbU3EplM6U9d8bq45q93LPg5hDxxDjIN87HIoAEM2KMxLLgctb+QpN8K1wIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private String notify_url = "http://jsqoi41ai7.54http.tech/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private String sign_type = "RSA2";

    // 字符编码格式
    private String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应：" + result);

        return result;

    }
}
